/* 2014/10/24 */
package com.cosmos.quartz.control.Db;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AgentControlDao {

public static AgentControlDao $ = new AgentControlDao();

	private static SqlMapper sqlMapper = new SqlMapper();

	private AgentControlDao() {
		
	}
	
	public void insertACRequest(DbControlRequest request) throws SQLException {
		sqlMapper.insert("insertACRequest", request);
	}
	
	public void insertACResponse(DbControlResponse response) throws SQLException {
		sqlMapper.insert("insertACResponse", response);
	}
	
	public void insertBatchACResponse(List<DbControlResponse> responses) throws SQLException {
		if (responses == null) {
			return;
		}		
		
		try {
			sqlMapper.startTransaction();
			sqlMapper.startBatch();
			for (DbControlResponse response : responses) {
				insertACResponse(response);
			}
			sqlMapper.executeBatch();
			sqlMapper.commitTransaction();
		} catch (SQLException ex) {
			throw ex;
		} finally {
			sqlMapper.endTransaction();
		}
	}
	
	/**
	 * Retrieves control requests to specified agent name, which have not been responded by this agent.
	 * @param agentName - agent name.
	 * @param includeBroadcase - whether retrieve broadcast request (agent name is null) or not.
	 * @param startTime - start time of retrieved requests.
	 */
	public List<DbControlRequest> retrieveACRequestNoResponseByAgent(String agentName
			, boolean includeBroadcast, Date startTime) throws SQLException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("agent", agentName);
		map.put("includeBroadcast", includeBroadcast);
		map.put("startTime", startTime);
		return (List<DbControlRequest>)sqlMapper.queryForList("retrieveACRequestNoResponseByAgent", map);
	}
	
	/**
	 * Retrieves control responses by specified agent name.
	 * @param agentName - agent name.
	 */
	public List<DbControlResponse> retrieveACResponseByAgent(String agentName) throws SQLException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("agent", agentName);
		return (List<DbControlResponse>)sqlMapper.queryForList("retrieveACResponseByAgent", map);
	}
	
	/**
	 * Retrieve the last request whose operation is specified value.
	 */
	public List<DbControlRequest> retrieveLastACRequestByOperation(String operation) throws SQLException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("operation", operation);
		return (List<DbControlRequest>)sqlMapper.queryForList("retrieveLastACRequestByOperation", map);
	}
	
	/**
	 * Retrieves responses from all agents to a specified request.
	 */
	public List<DbControlResponse> retrieveACResponseByRequestId(int requestId) throws SQLException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("requestId", requestId);
		return (List<DbControlResponse>)sqlMapper.queryForList("retrieveACResponseToRequest", map);
	}
	
	/**
	 * Retrieves responses to requests for displaying reports.
	 * Every response contains report.
	 * There are two types of request: for all jobs' operation and for single job operation.
	 * Response of 'all' request contains report of all job.
	 * Response of 'single' request contains report of single job.
	 * Same type of report should be overwritten by the last one, so only the last one will be returned.
	 * @param startTime - start time of responses in result.
	 */
	public List<DbControlResponseToRequest> retrieveACResponseToRequestForReport(Date startTime) throws SQLException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startTime", startTime);
		return (List<DbControlResponseToRequest>)sqlMapper.queryForList("retrieveACResponseToRequestForReport", map);
	}
	
}
